package io.renren.modules.study.controller;

import io.renren.common.annotation.Login;
import io.renren.common.utils.R;
import io.renren.modules.study.entity.CourseInfoAndPlanEntity;
import io.renren.modules.study.entity.CoursePlanEntity;
import io.renren.modules.study.entity.CoursePlanScheduleEntity;
import io.renren.modules.study.service.CoursePlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课程计划
 */
@RestController
@RequestMapping("api/coursePlan")
public class CoursePlanController {

    @Autowired
    CoursePlanService coursePlanService;
    /**
     * 创建一个计划
     */
    @PostMapping("createCoursePlan")
    @Login
    public R createCoursePlan(@ApiIgnore @RequestAttribute("userId") Long userId,@RequestBody Map map){
        map.put("userId",userId);
        List coursePlanList = coursePlanService.getCoursePlan(map);
        if(coursePlanList.size()>0){
                //说明这个人已经有计划了
            return R.error(101,"您已经有计划了哦");
        }
        if(null ==map.get("endTime") || null == map.get("startTime")){
            return R.error(101,"请完善计划时间信息");
        }
        coursePlanService.createCoursePlan(map);
        return R.ok("创建计划成功",100);
    }



    /**
     * 删除计划
     */
    @PostMapping("delCoursePlan")
    @Login
    public R delCoursePlan(@ApiIgnore @RequestAttribute("userId") Long userId){
        Map map = new HashMap();
        map.put("userId",userId);
        List coursePlanList = coursePlanService.getCoursePlan(map);
        if(coursePlanList.size()<= 0 ){
            return R.error(101,"您还没有属于自己的计划哦~");
        }
        CoursePlanEntity coursePlan = (CoursePlanEntity)coursePlanList.get(0);
        map.put("coursePlanId",coursePlan.getId());
        coursePlanService.delCoursePlan(map);
        return R.ok("删除成功",100);
    }

    /**
     * 修改课程计划
     */
    @PostMapping("updateCoursePlan")
    @Login
    public R updateCoursePlan(@ApiIgnore @RequestAttribute("userId") Long userId,@RequestBody Map map){
        map.put("userId",userId);
        int isSmallOrPlan = Integer.valueOf(map.get("isSmallOrPlan").toString());
        if(isSmallOrPlan == 1){
            //是个计划 不是微专业计划
            List coursePlanList = coursePlanService.getCoursePlan(map);
            CoursePlanEntity coursePlan = (CoursePlanEntity)coursePlanList.get(0);
            map.put("coursePlanId",coursePlan.getId());
            coursePlanService.updateCoursePlan(map);
            return R.ok("修改成功",100);
        }
        return R.error(101,"微专业计划不可修改信息哦");


    }

    /**
     * 查询我的计划
     */
    @PostMapping("getCoursePlan")
    @Login
    public R getCoursePlan(@ApiIgnore @RequestAttribute("userId") Long userId){
        Map map = new HashMap();
        map.put("userId",userId);
        List coursePlanList = coursePlanService.getCoursePlan(map);
        //判断计划的时间是否过期
        /*if(coursePlanList.size()>0){
            CoursePlanEntity coursePlan = (CoursePlanEntity)coursePlanList.get(0);
            Long endTime =  coursePlan.getEndTime().getTime();
            Date date = new Date();
            Long nowTime = date.getTime();
            boolean a = nowTime >endTime;
            System.out.println(a);
            if(a){
                //这个计划过期 删除这个计划
                map.put("coursePlanId",coursePlan.getId());
                coursePlanService.delCoursePlan(map);
                coursePlanList.remove(0);
            }
        }*/

        return R.ok("查询我的计划成功",100).put("data",coursePlanList);
    }

    /**
     * 将课程加入到我的计划中
     */
    @PostMapping("makeCourseInCoursePlan")
    @Login
    public R makeCourseInCoursePlan(@ApiIgnore @RequestAttribute("userId") Long userId,@RequestBody Map map){
        //获取我的计划
        map.put("userId",userId);
        List coursePlanList = coursePlanService.getCoursePlan(map);

        if(coursePlanList.size()<= 0){
            //还没创建计划 直接创建计划 同时将课程传输到我的计划中
            createCoursePlan(userId,map);
            List coursePlanList1 = coursePlanService.getCoursePlan(map);
            CoursePlanEntity coursePlan = (CoursePlanEntity)coursePlanList1.get(0);
            map.put("coursePlanId",coursePlan.getId());
            CoursePlanScheduleEntity coursePlanScheduleEntity =coursePlanService.checkCourseInMyPlan(map);
            if(null == coursePlanScheduleEntity){
                coursePlanService.makeCourseInCoursePlan(map);
                coursePlanService.updateTime(map);
            }
            return R.ok("创建计划成功",100);
        }else{
            CoursePlanEntity coursePlan = (CoursePlanEntity)coursePlanList.get(0);
            map.put("coursePlanId",coursePlan.getId());
            CoursePlanScheduleEntity coursePlanScheduleEntity =coursePlanService.checkCourseInMyPlan(map);
            if(null == coursePlanScheduleEntity){
                coursePlanService.makeCourseInCoursePlan(map);
                coursePlanService.updateTime(map);
                return R.ok("加入成功",100);
            }
            return R.error(101,"您的计划中已经有这个课程了哦");
        }
    }

    /**
     * 将课程从我的计划中删除
     */
    @PostMapping("delCourseInMyCoursePlan")
    @Login
    public R delCourseInMyCoursePlan(@ApiIgnore @RequestAttribute("userId") Long userId,@RequestBody Map map){
        map.put("userId",userId);
        List coursePlanList = coursePlanService.getCoursePlan(map);
        CoursePlanEntity coursePlan = (CoursePlanEntity)coursePlanList.get(0);
        map.put("coursePlanId",coursePlan.getId());
        coursePlanService.delCourseInMyCoursePlan(map);
        return R.ok("删除成功",100);
    }

    /**
     * 获取我的计划中的课程
     */
    @PostMapping("getCourseInMyPlan")
    @Login
    public R getCourseInMyPlan(@ApiIgnore @RequestAttribute("userId") Long userId, @RequestBody Map map) throws ParseException {
        String date = map.get("dateTime").toString();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dateT = sdf.parse(date);
       // Long dateTime = dateT.getTime();
        map.put("userId",userId);
        List coursePlanList = coursePlanService.getCoursePlan(map);
        if(coursePlanList.size()<= 0 ){
            return R.ok("",100);
        }
        CoursePlanEntity coursePlan = (CoursePlanEntity)coursePlanList.get(0);
        map.put("coursePlanId",coursePlan.getId());
        List list = coursePlanService.getCourseInMyPlan(map);


        for (int s = 0; s < list.size(); s++) {
            CourseInfoAndPlanEntity courseInfoAndPlanEntity = (CourseInfoAndPlanEntity) list.get(s);
            /*Long small = dateTime-courseInfoAndPlanEntity.getStartTime().getTime();
            Long big = courseInfoAndPlanEntity.getEndTime().getTime()-courseInfoAndPlanEntity.getStartTime().getTime();*/
            /*double timeSchedule1 = ((small*1.0/big)*1000);
            String t = timeSchedule1+"";
            String ss = t.substring(0,2);
            if("-".equals(ss.substring(0,1))){
                ss = "0";
            }*/
            BigDecimal b1 = new BigDecimal(courseInfoAndPlanEntity.getStartTime().getTime());
            BigDecimal b2 = new BigDecimal(courseInfoAndPlanEntity.getEndTime().getTime());
            BigDecimal bc = new BigDecimal(dateT.getTime());
            DecimalFormat df = new DecimalFormat("0%");
            /*//设置最大小数位
            df.setMaximumFractionDigits(1);
            //设置四舍五入方式
            df.setRoundingMode(RoundingMode.HALF_UP);*/
            BigDecimal bMiuns = bc.subtract(b1);
            BigDecimal bMiunsAll = b2.subtract(b1);
            //除法
            BigDecimal bOk = bMiuns.divide(bMiunsAll, 2, RoundingMode.HALF_UP);
            String bOk1 =df.format(bOk);
            String yy = bOk1.replace("%","");
            courseInfoAndPlanEntity.setTimeSchedule(yy);
        }
        return R.ok().put("data",list).put("count",getCourseCountInMyPlan(map));
    }

    /**
     * 获取不在我的计划中的
     */
    @GetMapping("getCourseNotInMyPlan")
    @Login
    public R getCourseNotInMyPlan(@ApiIgnore @RequestAttribute("userId") Long userId){
        Map map=new HashMap();
        map.put("userId",userId);
        List list = coursePlanService.getCourseNotInMyPlan(map);
        return R.ok().put("data",list);
    }

    /**
     * 获取在计划中的课程数量
     */
    public int getCourseCountInMyPlan(Map map){
        int count = coursePlanService.getCourseCountInMyPlan(map);
        return count;
    }

    /**
     * 修改这门课程的开始结束时间
     */
    @PostMapping("updateTime")
    @Login
    public R updateTime(@ApiIgnore @RequestAttribute("userId") Long userId,@RequestBody Map map){

        map.put("userId",userId);
        coursePlanService.updateTime(map);

        return R.ok("修改成功",100);
    }
}
